/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.android.provider;

import java.util.ArrayList;
import java.util.Collections;

import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;
import android.text.format.DateUtils;

import com.phonebooksharing.android.util.GeneralUtils;

public final class PhonebookSharing {
    public static final String AUTHORITY = "phonebooksharing";

    /**
     * Dashboard table which contains data ready to be displayed No calculations
     * needed here
     */
    public static final class Dashboard implements BaseColumns {
        // This class cannot be instantiated
        Dashboard() {
        }

        static final String        CONTENT_NAME           = "dashboard";

        /**
         * The content:// style URI for this table
         */
        public static final Uri    CONTENT_URI            = Uri.parse("content://" + AUTHORITY + "/" + CONTENT_NAME);

        /**
         * The MIME type of {@link #CONTENT_URI} providing a directory of
         * dashboard.
         */
        public static final String CONTENT_TYPE           = "vnd.android.cursor.dir/vnd." + AUTHORITY + "." + CONTENT_NAME;

        /**
         * Quantity of friends that are nearby the current location
         * <P>
         * Type: INTEGER
         * </P>
         */
        public static final String QTY_FRIENDS_NEARBY     = "fn";

        /**
         * Quantity of messages
         * <P>
         * Type: INTEGER
         * </P>
         */
        public static final String QTY_MESSAGES           = "m";

        /**
         * Quantity of new post cards
         * <P>
         * Type: INTEGER
         * </P>
         */
        public static final String QTY_POSTCARDS          = "pc";

        /**
         * Quantity of places that are nearby the current location
         * <P>
         * Type: INTEGER
         * </P>
         */
        public static final String QTY_PLACES_NEARBY      = "pn";

        /**
         * Quantity of photos
         * <P>
         * Type: INTEGER
         * </P>
         */
        public static final String QTY_PHOTOS             = "p";

        /**
         * Quantity of activities
         * <P>
         * Type: INTEGER
         * </P>
         */
        public static final String QTY_JOURNAL            = "a";

        /**
         * Quantity of videos
         * <P>
         * Type: INTEGER
         * </P>
         */
        public static final String QTY_VIDEOS             = "v";

        /**
         * Non Sharing Images
         * <P>
         * Type: INTEGER
         * </P>
         */
        public static final String QTY_NON_SHARING_IMAGES = "ni";

        /**
         * Non Sharing Videos
         * <P>
         * Type: INTEGER
         * </P>
         */
        public static final String QTY_NON_SHARING_VIDEOS = "vi";

        public int                 qtyFriendsNearby, qtyMessages, qtyPostcards, qtyPlacesNearby, qtyPhotos, qtyJournal, qtyVideos, qtyNonSharingImages, qtyNonSharingVideos;

        public static Dashboard fromCursor(Cursor c) {
            final Dashboard mi = new Dashboard();
            mi.qtyFriendsNearby = c.getInt(c.getColumnIndexOrThrow(QTY_FRIENDS_NEARBY));
            mi.qtyMessages = c.getInt(c.getColumnIndexOrThrow(QTY_MESSAGES));
            mi.qtyPostcards = c.getInt(c.getColumnIndexOrThrow(QTY_POSTCARDS));
            mi.qtyPlacesNearby = c.getInt(c.getColumnIndexOrThrow(QTY_PLACES_NEARBY));
            mi.qtyPhotos = c.getInt(c.getColumnIndexOrThrow(QTY_PHOTOS));
            mi.qtyJournal = c.getInt(c.getColumnIndexOrThrow(QTY_JOURNAL));
            mi.qtyVideos = c.getInt(c.getColumnIndexOrThrow(QTY_VIDEOS));
            mi.qtyNonSharingImages = c.getInt(c.getColumnIndexOrThrow(QTY_NON_SHARING_IMAGES));
            mi.qtyNonSharingVideos = c.getInt(c.getColumnIndexOrThrow(QTY_NON_SHARING_VIDEOS));
            return mi;
        }
    }

    /**
     * Sync table which contains data ready to be displayed No calculations
     * needed here
     */
    public static final class Sync implements BaseColumns {
        // This class cannot be instantiated
        Sync() {
        }

        static final String        CONTENT_NAME      = "sync";

        /**
         * The content:// style URI for this table
         */
        public static final Uri    CONTENT_URI       = Uri.parse("content://" + AUTHORITY + "/" + CONTENT_NAME);

        /**
         * The MIME type of {@link #CONTENT_URI} providing a directory of sync.
         */
        public static final String CONTENT_TYPE      = "vnd.android.cursor.dir/vnd." + AUTHORITY + "." + CONTENT_NAME;

        /**
         * The MIME type of a {@link #CONTENT_URI} sub-directory of a single
         * sync.
         */
        public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd." + AUTHORITY + "." + CONTENT_NAME;

        /**
         * Timestamp of the sync
         * <P>
         * Type: INTEGER
         * </P>
         */
        public static final String SYNC_TIME         = "st";

        /**
         * Sync Latitude
         * <P>
         * Type: TEXT
         * </P>
         */
        public static final String SYNC_LATITUDE     = "sla";

        /**
         * Sync Longitude
         * <P>
         * Type: TEXT
         * </P>
         */
        public static final String SYNC_LONGITUDE    = "slo";

        /**
         * Sync Address
         * <P>
         * Type: TEXT
         * </P>
         */
        public static final String SYNC_ADDRESS      = "sa";

        /**
         * Sync Status
         * <P>
         * Type: NUMBER
         * </P>
         */
        public static final String SYNC_STATUS       = "ss";

        /**
         * Sync Type
         * <P>
         * Type: NUMBER
         * </P>
         */
        public static final String SYNC_TYPE         = "stp";

        public static final int    STATUS_PROCESSING = 0, STATUS_SUCCESS = 1, STATUS_FAILURE = 2, STATUS_NOT_COMPLETED = 3;

        public static final int    SYNC_TYPE_AUDIO   = 1, SYNC_TYPE_VIDEO = 2, SYNC_TYPE_IMAGE = 3;

        public String              syncLat, syncLong, syncAddress;
        public long                syncTime;
        public int                 syncStatus, syncType;

        /**
         * @return the syncStatus
         */
        public String getSyncStatusDesc() {
            return getSyncStatus(syncStatus);
        }

        /**
         * @return the syncStatus
         */
        static String getSyncStatus(int status) {
            String s = null;
            switch (status) {
            case STATUS_PROCESSING:
                s = "Sync in Progress";
                break;
            case STATUS_SUCCESS:
                s = "Sync Success";
                break;
            case STATUS_FAILURE:
                s = "Sync Fail";
                break;
            case STATUS_NOT_COMPLETED:
                s = "Sync Not Completed";
                break;
            default:
                break;
            }
            return s;
        }

        public static ArrayList<Sync> fromCursor(Cursor c) {
            ArrayList<Sync> list = new ArrayList<Sync>();
            if (c.moveToFirst()) {
                int st = c.getColumnIndexOrThrow(SYNC_TIME), sa = c.getColumnIndexOrThrow(SYNC_ADDRESS), sla = c.getColumnIndexOrThrow(SYNC_LATITUDE), ss = c.getColumnIndexOrThrow(SYNC_STATUS), slo = c
                        .getColumnIndexOrThrow(SYNC_LONGITUDE), stp = c.getColumnIndexOrThrow(SYNC_TYPE);
                Sync sync;
                do {
                    sync = new Sync();
                    sync.syncTime = c.getLong(st);
                    sync.syncAddress = c.getString(sa);
                    sync.syncLat = c.getString(sla);
                    sync.syncLong = c.getString(slo);
                    sync.syncStatus = c.getInt(ss);
                    sync.syncType = c.getInt(stp);

                    list.add(sync);
                } while (c.moveToNext());
            }
            return list;
        }
    }

    /**
     * Message sync table which contains info that needs to be sync with PBSs
     * servers
     * 
     */
    public static final class Message implements BaseColumns {
        // This class cannot be instantiated
        Message() {
        }

        static final String        CONTENT_NAME      = "message";

        /**
         * The content:// style URI for this table
         */
        public static final Uri    CONTENT_URI       = Uri.parse("content://" + AUTHORITY + "/" + CONTENT_NAME);

        /**
         * The MIME type of {@link #CONTENT_URI} providing a directory of
         * message.
         */
        public static final String CONTENT_TYPE      = "vnd.android.cursor.dir/vnd." + AUTHORITY + "." + CONTENT_NAME;

        /**
         * The MIME type of a {@link #CONTENT_URI} sub-directory of a single
         * message.
         */
        public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd." + AUTHORITY + "." + CONTENT_NAME;

        /**
         * Message Type: </br> 0 - Text</br> 1 - Voice
         * <P>
         * Type: INTEGER
         * </P>
         */
        public static final String TYPE              = "t";

        /**
         * Message Title
         * <P>
         * Type: String
         * </P>
         */
        public static final String TITLE             = "tl";

        /**
         * Message Text
         * <P>
         * Type: String
         * </P>
         */
        public static final String TEXT              = "tx";

        public static final int    MSG_TYPE_TEXT     = 0, MSG_TYPE_VOICE = 1;
    }

    /**
     * This table stores the contacts that the sync objects are going to be send
     * to. For example: Message has been send to several contacts
     * 
     */
    public static final class SendTo implements BaseColumns {
        // This class cannot be instantiated
        SendTo() {
        }

        static final String        CONTENT_NAME      = "sendto";

        /**
         * The content:// style URI for this table
         */
        public static final Uri    CONTENT_URI       = Uri.parse("content://" + AUTHORITY + "/" + CONTENT_NAME);

        /**
         * The MIME type of {@link #CONTENT_URI} providing a directory of
         * message.
         */
        public static final String CONTENT_TYPE      = "vnd.android.cursor.dir/vnd." + AUTHORITY + "." + CONTENT_NAME;

        /**
         * The MIME type of a {@link #CONTENT_URI} sub-directory of a single
         * message.
         */
        public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd." + AUTHORITY + "." + CONTENT_NAME;

        public static final int    RELATION_TO_MESSAGE = 0, RELATION_TO_VOICE = 1;

        /**
         * Relation table : </br> 0 - Message</br> </br> 1 - Voice</br>
         * <P>
         * Type: INTEGER
         * </P>
         */
        public static final String RELATE_TABLE        = "rt";

        /**
         * Related table ID
         * <P>
         * Type: String
         * </P>
         */
        public static final String RELATE_ID           = "ri";

        /**
         * Has been sent to group?
         * <P>
         * Type: INTEGER
         * </P>
         */
        public static final String SENT_TO_GROUP       = "sg";

        /**
         * Contact ID to whom this was sent to
         * <P>
         * Type: INTEGER
         * </P>
         */
        public static final String CONTACT_ID          = "c";

        /**
         * Group ID to whom this was sent to
         * <P>
         * Type: INTEGER
         * </P>
         */
        public static final String GROUP_ID            = "g";
    }

    public static final class DashboardSync implements BaseColumns {
        // This class cannot be instantiated
        DashboardSync() {
        }

        static final String        CONTENT_NAME      = "dash_sync";

        /**
         * The content:// style URI for this table
         */
        public static final Uri    CONTENT_URI       = Uri.parse("content://" + AUTHORITY + "/" + CONTENT_NAME);

        /**
         * The MIME type of {@link #CONTENT_URI} providing a directory of
         * message.
         */
        public static final String CONTENT_TYPE      = "vnd.android.cursor.dir/vnd." + AUTHORITY + "." + CONTENT_NAME;

        /**
         * The MIME type of a {@link #CONTENT_URI} sub-directory of a single
         * message.
         */
        public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd." + AUTHORITY + "." + CONTENT_NAME;

        /**
         * Dashboard Item ID
         * <P>
         * Type: INTEGER
         * </P>
         */
        public static final String DASHBOARD_ITEM_ID = "id";

        /**
         * Sync Action </br> 0 - Archive</br> 1 - Add</br> 2 - Delete</br>
         * <P>
         * Type: INTEGER
         * </P>
         */
        public static final String ACTION            = "a";

        public static final int    ACTION_ARCHIVE    = 0, ACTION_DELETE = 1;
    }

    public static final class Settings implements BaseColumns {
        // This class cannot be instantiated
        Settings() {
        }

        static final String        CONTENT_NAME          = "setting";

        /**
         * The content:// style URI for this table
         */
        public static final Uri    CONTENT_URI           = Uri.parse("content://" + AUTHORITY + "/" + CONTENT_NAME);

        /**
         * The MIME type of {@link #CONTENT_URI} providing a directory of
         * message.
         */
        public static final String CONTENT_TYPE          = "vnd.android.cursor.dir/vnd." + AUTHORITY + "." + CONTENT_NAME;

        /**
         * The MIME type of a {@link #CONTENT_URI} sub-directory of a single
         * message.
         */
        public static final String CONTENT_ITEM_TYPE     = "vnd.android.cursor.item/vnd." + AUTHORITY + "." + CONTENT_NAME;

        /**
         * Authentication ID
         * <P>
         * Type: TEXT
         * </P>
         */
        public static final String AUTH_ID               = "ai";

        /**
         * Image Gallery Counter
         * <P>
         * Type: INTEGER
         * </P>
         */
        public static final String IMAGE_GALLERY_COUNTER = "ig";

        /**
         * Voice Gallery Counter
         * <P>
         * Type: INTEGER
         * </P>
         */
        public static final String VOICE_GALLERY_COUNTER = "vg";

    }

    /**
     * Message that is gonna be displayed on the widget
     * 
     */
    public static final class DesktopMessage implements BaseColumns, Comparable<DesktopMessage> {
        // This class cannot be instantiated
        DesktopMessage() {
        }

        static final String          CONTENT_NAME          = "desktop_message";

        /**
         * The content:// style URI for this table
         */
        public static final Uri      CONTENT_URI           = Uri.parse("content://" + AUTHORITY + "/" + CONTENT_NAME);

        /**
         * The MIME type of {@link #CONTENT_URI} providing a directory of
         * message.
         */
        public static final String   CONTENT_TYPE          = "vnd.android.cursor.dir/vnd." + AUTHORITY + "." + CONTENT_NAME;

        /**
         * The MIME type of a {@link #CONTENT_URI} sub-directory of a single
         * message.
         */
        public static final String   CONTENT_ITEM_TYPE     = "vnd.android.cursor.item/vnd." + AUTHORITY + "." + CONTENT_NAME;

        /**
         * Message Type: </br> 0 - Text</br> 1 - Voice
         * <P>
         * Type: INTEGER
         * </P>
         */
        public static final String   TYPE                  = "t";

        /**
         * Message Title
         * <P>
         * Type: String
         * </P>
         */
        public static final String   TITLE                 = "tl";

        /**
         * Message: Contact name
         * <P>
         * Type: String
         * </P>
         */
        public static final String   CONTACT_NAME          = "c";

        /**
         * Message: When
         * <P>
         * Type: Integer
         * </P>
         */
        public static final String   WHEN                  = "w";

        public String                contactName, title, whenFormatted;
        public long                  when, delta;

        public static final String[] PROJECTION_APPWIDGETS = new String[] { DesktopMessage.TITLE, DesktopMessage.CONTACT_NAME, DesktopMessage.WHEN, };

        public static final int      MSG_TYPE_TEXT         = 0, MSG_TYPE_VOICE = 1;

        public static ArrayList<DesktopMessage> fromCursor(Cursor c) {
            ArrayList<DesktopMessage> list = new ArrayList<DesktopMessage>();
            long w, cs;

            if (c.moveToFirst()) {
                DesktopMessage dm;
                do {
                    dm = new DesktopMessage();
                    dm.title = c.getString(0);
                    dm.contactName = c.getString(1);
                    dm.when = c.getLong(2);
                    w = GeneralUtils.calculateGmtTimeInMilis(dm.when);
                    cs = GeneralUtils.calculateGmtTimeInMilis(System.currentTimeMillis());

                    dm.whenFormatted = "(" + DateUtils.formatElapsedTime((cs - w) / 1000) + ")";

                    dm.delta = (w - cs);
                    list.add(dm);
                } while (c.moveToNext());
            }
            Collections.sort(list);
            return list;
        }

        @Override
        public int compareTo(DesktopMessage another) {
            return (int) (delta - another.delta);
        }
    }

    /**
     * Message that is gonna be displayed on the widget
     * 
     */
    public static final class DesktopWidgets implements BaseColumns {
        // This class cannot be instantiated
        DesktopWidgets() {
        }

        static final String        CONTENT_NAME      = "desktop_widgets";

        /**
         * The content:// style URI for this table
         */
        public static final Uri    CONTENT_URI       = Uri.parse("content://" + AUTHORITY + "/" + CONTENT_NAME);

        /**
         * The MIME type of {@link #CONTENT_URI} providing a directory of
         * message.
         */
        public static final String CONTENT_TYPE      = "vnd.android.cursor.dir/vnd." + AUTHORITY + "." + CONTENT_NAME;

        /**
         * The MIME type of a {@link #CONTENT_URI} sub-directory of a single
         * message.
         */
        public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd." + AUTHORITY + "." + CONTENT_NAME;

        /**
         * Message Title
         * <P>
         * Type: String
         * </P>
         */
        public static final String UPDATE_INTERVAL   = "ui";

        /**
         * Message Title
         * <P>
         * Type: String
         * </P>
         */
        public static final String IS_CONFIGURED     = "ic";
    }
}
